package com.douples.citywide.adapter;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.douples.citywide.R;
import com.douples.citywide.holder.MainViewHolder;

/**
 * Created by liuwei on 2017/7/6.
 */

public class BannerLayoutAdapter extends SubAdapter {

    private RecyclerView.RecycledViewPool recycledViewPool;
    private Context context;

    public BannerLayoutAdapter(Context context, LayoutHelper layoutHelper, int count, RecyclerView.RecycledViewPool pool) {
        super(context, layoutHelper, count);
        this.recycledViewPool=pool;
        this.context=context;
    }

    @Override
    public void onViewRecycled(MainViewHolder holder) {
        super.onViewRecycled(holder);
        if (holder.itemView instanceof ViewPager) {
            ((ViewPager) holder.itemView).setAdapter(null);
        }
    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 1)
            return new MainViewHolder(
                    LayoutInflater.from(context).inflate(R.layout.view_pager, parent, false));

        return super.onCreateViewHolder(parent, viewType);
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }

    @Override
    protected void onBindViewHolderWithOffset(MainViewHolder holder, int position, int offsetTotal) {
    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        if (holder.itemView instanceof ViewPager) {
            ViewPager viewPager = (ViewPager) holder.itemView;
            viewPager.setLayoutParams(new VirtualLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 200));
            viewPager.setAdapter(new PagerAdapter(this, recycledViewPool));
        }
    }
}
